package com.qqtech.qquan.mall.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.enums.MallShopCategoryStatusDictEnum;
import com.qqtech.qquan.mall.model.MallShopCategory;
import com.qqtech.qquan.mall.model.MallShopCategoryQuery;
import com.qqtech.qquan.mall.model.MallShopCategoryVo;
import com.qqtech.qquan.mall.service.MallShopCategoryService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/mall/shop/category")
public class MallShopCategoryController extends
		BaseWebController<MallShopCategory, MallShopCategoryQuery> {
	@Resource
	private MallShopCategoryService mallShopCategoryService;

	@Override
	protected BaseService<MallShopCategory> getBaseService() {
		return mallShopCategoryService;
	}

	@RequestMapping(value = "/childrenList")
	@ResponseBody
	public Result childrenList(
			@RequestBody MallShopCategoryQuery mallShopCategoryQuery) {
		Result result = new Result();
		if (mallShopCategoryQuery == null) {
			result.addErr("参数不能为空");
			return result;
		}
		if (mallShopCategoryQuery.getParentId() == null) {
			mallShopCategoryQuery.setParentId("0");
		}
		String key = mallShopCategoryQuery.getParentId();
		if (mallShopCategoryQuery.getType() != null) {
			key += "_"+mallShopCategoryQuery.getType().toString();
		}
		List<MallShopCategoryVo> childrenList = mallShopCategoryService
				.queryListByParentIdCache(key);
		result.put("childrenList", childrenList);
		return result;
	}
	
	/**
	 * 用于列表页面上的查询条件
	 * @param mallShopCategoryQuery
	 * @return
	 */
	@RequestMapping(value = "/childrenListForPage")
	@ResponseBody
	public Result childrenListForPage(
			@RequestBody MallShopCategoryQuery mallShopCategoryQuery) {
		Result result = new Result();
		if (mallShopCategoryQuery == null) {
			result.addErr("参数不能为空");
			return result;
		}
		if (mallShopCategoryQuery.getParentId() == null) {
			mallShopCategoryQuery.setParentId("0");
		}
		List<MallShopCategoryVo> childrenList = mallShopCategoryService
				.queryListByParentIdCache(mallShopCategoryQuery.getParentId());
		if (childrenList == null) {
			childrenList = new ArrayList<MallShopCategoryVo>();
		}
		MallShopCategoryVo all = new MallShopCategoryVo();
		if (mallShopCategoryQuery.getParentId().equals("0")) {
			all.setCategoryId("");
		} else {
			all.setCategoryId(mallShopCategoryQuery.getParentId());
		}
		all.setName("全部");
		all.setSelected("1");
		childrenList.add(0, all);
		result.put("childrenList", childrenList);
		return result;
	}
	
	/**
	 * 判断是否是最后一层
	 * @param mallShopCategoryQuery
	 * @return 是返回isLast=1，否则isLast=0
	 */
	@RequestMapping(value = "/isLast")
	@ResponseBody
	public Result isLast(
			@RequestBody MallShopCategoryQuery mallShopCategoryQuery) {
		Result result = new Result();
		if (mallShopCategoryQuery == null) {
			result.addErr("参数不能为空");
			return result;
		}
		if (mallShopCategoryQuery.getCategoryId() == null) {
			result.addErr("参数id不能为空");
			return result;
		}
		MallShopCategoryQuery temp = new MallShopCategoryQuery();
		temp.setParentId(mallShopCategoryQuery.getCategoryId());
		temp.setStatus(MallShopCategoryStatusDictEnum.VALID
				.getCode());
		int v = mallShopCategoryService.queryCount(temp);
		if (v > 0) {
			result.put("isLast", 0);
		} else {
			result.put("isLast", 1);
		}
		return result;
	}
}
